package avl.sv.client.tools;

import avl.sv.client.MouseActionLogger;
import avl.sv.client.SlideViewer;
import avl.sv.shared.Annotations.Annotation;
import avl.sv.shared.Annotations.AnnotationSet;
import avl.sv.shared.Annotations.ROI;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreePath;

/**
 *
 * @author benbryan
 */
public class SelectTool extends AbstractSlideViewerTool {

    private MouseActionLogger mouseAction = new MouseActionLogger();

    final SlideViewer slideViewer;

    public SelectTool(SlideViewer slideViewer) {
        this.slideViewer = slideViewer;
        setToolTipText("Select ROIs");
        setIcon(new javax.swing.ImageIcon(getClass().getResource("/avl/sv/client/icon/select.png")));

    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }

        mouseAction.mouseClicked(e);
        AnnotationSet annoSet = slideViewer.getRoiManager().getAnnotationSet();
        if (annoSet == null) {
            return;
        }
        Point pImg = slideViewer.displaySpaceToImageSpace(e.getPoint());
        if (e.getButton() == MouseEvent.BUTTON1) {
            ArrayList<TreePath> paths = new ArrayList<>();
            for (Annotation anno : annoSet.getAnnotations()) {
                ArrayList<ROI> rois = anno.getRois();
                for (ROI roi : rois) {
                    if (roi.containsPoint(pImg)) {
                        Object o[] = new Object[3];
                        o[0] = annoSet;
                        o[1] = anno;
                        o[2] = roi;
                        paths.add(new TreePath(o));
                    }
                }
            }
            if ((paths.size()) > 0 && (mouseAction.getClickCount() > 0)) {
                // if click count is greater than one, then select a single ROI to highlight
                int idx = mouseAction.getClickCount() % paths.size();
                if (idx < paths.size()) {
                    ArrayList<TreePath> pathsTemp = new ArrayList<>();
                    pathsTemp.add(paths.get(idx));
                    paths = pathsTemp;
                }
            }
            slideViewer.getRoiManager().setSelectionPaths(paths);
        } else if (e.getButton() == MouseEvent.BUTTON3) {
            for (Annotation annotation : annoSet.getAnnotations()) {
                for (final ROI roi : annotation.getRois()) {
                    if (roi.containsPoint(pImg)) {
                        JPopupMenu menu = new JPopupMenu();

                        JMenuItem gotoROI = new JMenuItem("Goto");
                        gotoROI.addActionListener(new AbstractAction() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                Rectangle bounds = roi.getPolygon().getBounds();
                                slideViewer.setImageRegion(bounds);
                            }
                        });
                        menu.add(gotoROI);

                            JMenuItem delete = new JMenuItem("Delete");
                            delete.addActionListener(new AbstractAction() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    slideViewer.getRoiManager().removeROI(roi);
                                }
                            });
                            menu.add(delete);

                            JMenuItem export = new JMenuItem("Export");
                            export.addActionListener(new AbstractAction() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    slideViewer.getRoiManager().getExportDialogROIs().promptForExport(new Object[]{roi});
                                }
                            });
                            menu.add(export);

                        menu.show(slideViewer, e.getX(), e.getY());
                        return;
                    }
                }
            }
        }
        slideViewer.repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mouseReleased(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mouseDragged(e);

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mousePressed(e);

    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void paintOnSlideViewer(Graphics2D g) {
    }

}
